package cn.jiedh.app.meiweidk.Utils.UI;

import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.annotation.StyleRes;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;

import com.github.florent37.viewanimator.ViewAnimator;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import cn.jiedh.app.meiweidk.R;

/**
 * @name：
 * @author： liuyun
 * @phone： 15723310658
 * @createTime： 2017/9/1
 * @modifyTime： 2017/9/1
 * @explain：说明
 */


public class VerticalBannerView<T> extends RelativeLayout {
    private static final int ANIMATE_CHANGE=1;//开始动画
    private Context mContext;
    private int intevalTime=5*1000;
    private int animationTime=500;
    private int itemLayoutRes;
    private List<T> mDatas;
    private View chaildView1,chaildView2;
    private int currentIndex=0;
    private ViewHolder mViewHolder;
    private View mCurrentChaildView;
    private Timer mTimer=new Timer();
    private Handler mHandle;


    public interface ViewHolder<T>{
        public void initView(T t, View view);
    }
    public VerticalBannerView(@NonNull Context context) {
        super(context);
    }

    public VerticalBannerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs,0);
    }

    public VerticalBannerView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs,defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public VerticalBannerView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context,attrs,defStyleAttr);
    }

    public void setViewHolder(ViewHolder viewHolder) {
        this.mViewHolder = viewHolder;
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr){
        mContext=context;
        mHandle=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if(msg.what==ANIMATE_CHANGE){
                    animateChange();
                }
            }
        };
    }
    public void  setChildClick(OnClickListener onClickListener){
        chaildView1.setOnClickListener(onClickListener);
        chaildView2.setOnClickListener(onClickListener);
    }
    public void setItemView(int itemViewRes){
        this.itemLayoutRes=itemViewRes;
        chaildView1=View.inflate(mContext,itemLayoutRes,null);
        chaildView1.setId(R.id.vertical_banner_chaidl1);
        chaildView2=View.inflate(mContext,itemLayoutRes,null);
        chaildView2.setId(R.id.vertical_banner_chaidl2);
        addView(chaildView1);
        addView(chaildView2);
        chaildView1.bringToFront();

    }

    public void setDatas(List<T> list){
        this.mDatas=list;
        initChaildViews();
    }
    private void initChaildViews(){
        if(mViewHolder!=null && mDatas!=null && mDatas.size()>0){
            currentIndex=0;
            chaildView1.setTranslationY(0);
            chaildView2.setTranslationY(0);
            chaildView1.bringToFront();
            mCurrentChaildView=chaildView1;
            mViewHolder.initView(mDatas.get(currentIndex),chaildView1);
            if(mDatas.size()>1){
                mViewHolder.initView(mDatas.get(currentIndex+1),chaildView2);
            }
        }
    }
    public void start(){
//         if(mDatas!=null && mDatas.size()>1) {
//             postDelayed(mRunable, intevalTime);
//         }


        stop();
        mTimer=new Timer();
        if(mDatas!=null && mDatas.size()>1)
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
//                animateChange();
                mHandle.sendEmptyMessage(ANIMATE_CHANGE);
            }
        },0,intevalTime);

    }

    private void animateChange(){
        View otherView;
        if (mCurrentChaildView == chaildView1) {
            otherView = chaildView2;
        } else {
            otherView = chaildView1;
        }
        ViewAnimator
                .animate(mCurrentChaildView)
                .translationY(0, -getHeight())
                .andAnimate(otherView)
                .dp()
                .translationY(getHeight(), 0)
                .decelerate()
                .duration(animationTime)
                .start().onStop(()-> {
//                    View temp=mCurrentChaildView;
            mViewHolder.initView(mDatas.get((currentIndex+2)%mDatas.size()),mCurrentChaildView);
            mCurrentChaildView=otherView;
            currentIndex++;
        });
//        ViewAnimator
//                .animate(mCurrentChaildView)
//                .translationY(0, getHeight())
//                .andAnimate(otherView)
//                .dp()
//                .translationY(0 - getHeight(), 0)
//                .decelerate()
//                .duration(animationTime)
//                .start().onStop(()-> {
////                    View temp=mCurrentChaildView;
//                mViewHolder.initView(mDatas.get((currentIndex+2)%mDatas.size()),mCurrentChaildView);
//                mCurrentChaildView=otherView;
//                currentIndex++;
//            });

    }

//    private Runnable mRunable = new Runnable() {
//        @Override
//        public void run() {
//            View otherView;
//            if (mCurrentChaildView == chaildView1) {
//                otherView = chaildView2;
//            } else {
//                otherView = chaildView1;
//            }
//            ViewAnimator
//                    .animate(mCurrentChaildView)
//                    .waitForHeight()
//                    .translationY(0, getHeight())
//                    .andAnimate(otherView)
//                    .dp()
//                    .translationY(0 - getHeight(), 0)
//                    .decelerate()
//                    .duration(animationTime)
//                    .start().onStop(new AnimationListener.Stop() {
//                @Override
//                public void onStop() {
////                    View temp=mCurrentChaildView;
//                    mViewHolder.initView(mDatas.get((currentIndex+2)%mDatas.size()),mCurrentChaildView);
//                    mCurrentChaildView=otherView;
//                    currentIndex++;
//                    postDelayed(mRunable, intevalTime);
//                }
//            });
//        }
//    };

    public void stop(){
//        removeCallbacks(mRunable);
        if(mTimer!=null)
        mTimer.cancel();
    }


}
